import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;


public class httpRequestHandler implements Runnable{
	
	
	final static String CRLF = "\r\n";
	Socket socket;
	InputStream input;
	OutputStream output;
	BufferedReader br;
	
	public httpRequestHandler(Socket socket) throws Exception
	{
		this.socket = socket;
		this.input = socket.getInputStream();
		this.output = socket.getOutputStream();
		this.br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
	}
	
	public void run()
	{
		try {
			processRequest();
		}catch(Exception e) {
			System.out.println(e);
		}
	}
	
	private void processRequest() throws Exception
	{
		while(true) {
			String headerLine = br.readLine();
			System.out.println("The client request is "+headerLine);
			if(headerLine.equals(CRLF) || headerLine.equals("")) break;
//			StringTokenizer s = new StringTokenizer(headerLine);
//			String temp = s.nextToken();
			String t[] = headerLine.split(" ");
			String temp = t[0];
			if(temp.equals("GET")) {
				String fileName = t[1];
				fileName = "." + fileName ;
				
				FileInputStream fis = null ;
				boolean fileExists = true ;
				try{
					fis = new FileInputStream( fileName ) ;
				}
				catch ( FileNotFoundException e ){
					fileExists = false ;
				}

				String serverLine = "Server: a simple java httpServer";
				String statusLine = null;
				String contentTypeLine = null;
				String entityBody = null;
				String contentLengthLine = "error";
				if ( fileExists ){
					statusLine = "HTTP/1.0 200 OK" + CRLF ;
					contentTypeLine = "Content-type: " + contentType( fileName ) + CRLF ;
					contentLengthLine = "Content-Length: " + (new Integer(fis.available())).toString() + CRLF;
				}else{
					statusLine = "HTTP/1.0 404 Not Found" + CRLF ;
					contentTypeLine = "text/html" ;
					entityBody = "404 Not Found"
					+"Usage:http://yourHostName:port/"
					+"fileName.htm" ;
				}
				output.write(statusLine.getBytes());
				output.write(serverLine.getBytes());
				output.write(contentTypeLine.getBytes());
				output.write(contentLengthLine.getBytes());
				output.write(CRLF.getBytes());
				
				if (fileExists){
					sendBytes(fis, output) ;
					fis.close();
				}else{
					output.write(entityBody.getBytes());
				}
			}
		}
		try {
			output.close();
			br.close();
			socket.close();
		}catch(Exception e) {}
		}
	
	private static void sendBytes(FileInputStream fis, OutputStream os) throws Exception
	{
		byte[] buffer = new byte[1024] ;
		int bytes = 0 ;
		while ((bytes = fis.read(buffer)) != -1 ){
			os.write(buffer, 0, bytes);
		}
	}
	
	private static String contentType(String fileName)
	{
		if (fileName.endsWith(".htm") || fileName.endsWith(".html")){
			return "text/html";
		}
		return "fileName";
	}
} 


